package visualizer.input.concrete;

import processing.core.PGraphics;
import processing.core.PImage;
import processing.core.PVector;
import visualizer.data.DataSource;
import visualizer.input.Component;

public class GraphicalComponent<T> extends Component {
	private PImage[] background;
	private DataSource<T> input;
	
	public GraphicalComponent(PVector position, PImage[] background, DataSource<T> input)
	{
		super(position, calculateSize(background));
		this.background = background;
		this.input = input;
	}
	
	private static PVector calculateSize(PImage[] images)
	{
		PVector size = new PVector();
		for (PImage i : images) {
			if (size.x < i.width) {
				size.x = i.width;
			}
			if (size.y < i.height) {
				size.y = i.height;
			}
		}
		return size;
	}
	
	public DataSource<T> getInput()
	{
		return input;
	}
	
	public int getState()
	{
		if (hasFocus()) {
			return 2;
		}
		if (isHovered()) {
			return 1;
		}
		return 0;
	}

	protected void drawRelative(PGraphics g) {
		int state = getState();
		if ((background.length > state) && (null != background[state])) {
			g.image(background[state], 0, 0);
		}
	}
}
